package lab1;

public class RungeKuttaMethod {
    public static void main(String[] args) {
        System.out.println("Runge-Kutta: h = 0.1 || n = 10");
        rungeKuttaMethod(1, 1, 0.1, 10);
        System.out.println("Runge-Kutta: h = 0.2 || n = 10");
        rungeKuttaMethod(1, 1, 0.1, 10);
    }

    private static void rungeKuttaMethod(double x0, double u0, final double h, final int n) {
        for (int i = 0; i <= n; i++) {
            double newX = x0 + h;
            double k1 = h * (-3 * u0 * u0 * x0 * x0);
            double k2 = h * (-3 * (x0 + h / 2) * (x0 + h / 2) * (u0 + k1 / 2) * (u0 + k1 / 2));
            double k3 = h * (-3 * (x0 + h / 2) * (x0 + h / 2) * (u0 + k2 / 2) * (u0 + k2 / 2));
            double k4 = h * (-3 * (x0 + h) * (x0 + h) * (u0 + k3) * (u0 + k3));
            double newU = u0 + (k1 + 2 * k2 + 2 * k3 + k4) / 6;
            System.out.println("New U : " + newU);
            x0 = newX;
            u0 = newU;
        }
    }
}
